package com.tbit.main.pojo.maintain_performance.group;

import com.tbit.main.constant.enums.ConditionLogic;
import com.tbit.main.constant.enums.PerformanceBizType;
import com.tbit.main.constant.enums.PerformanceRuleType;
import com.tbit.main.pojo.PerformanceRule;
import com.tbit.main.pojo.PerformanceRuleCondition;
import com.tbit.main.pojo.PerformanceRuleGroup;
import com.tbit.main.pojo.maintain_performance.Range;
import com.tbit.main.pojo.maintain_performance.BizRuleGroup;
import com.tbit.main.pojo.maintain_performance.rule.ValidityBasedRule;
import lombok.Data;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 换电产生订单时效配置
 *
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2025/2/19
 */
@Data
public class ValidityBasedGroup implements BizRuleGroup {
    /**
     * 是否启用
     */
    private Boolean enabled;
    /**
     * 任务完成时间范围
     */
    private List<Range<LocalTime>> completedDuration;
    /**
     * 24小时内最多奖励订单数
     */
    private Integer maxOrderCount;
    /**
     * 时效对应绩效配置
     */
    private List<ValidityBasedRule> subRules;

    @Override
    public PerformanceRuleGroup toRaw(PerformanceBizType bizType, Integer accountId) {
        PerformanceRuleGroup group = new PerformanceRuleGroup();
        group.setAccountId(accountId);
        group.setBizType(bizType);
        group.setRuleType(PerformanceRuleType.VALIDITY);
        group.setEnabled(enabled);
        group.setRules(new ArrayList<>());

        if (subRules == null) {
            return group;
        }
        for (ValidityBasedRule rule : subRules) {
            PerformanceRule performanceRule = new PerformanceRule();
            performanceRule.setPerformance(rule.getPerformance());
            performanceRule.setConditionLogic(ConditionLogic.AND);
            performanceRule.setConditions(new ArrayList<>());

            // 任务完成时间条件
            performanceRule.getConditions().add(Range.combine("completedTime", completedDuration, ConditionLogic.OR));

            // 产生订单时效条件
            performanceRule.getConditions().add(rule.getOrderTimeHour().toCondition("orderTimeFromCompleted"));

            group.getRules().add(performanceRule);
        }

        Map<String, Object> metaInfo = new HashMap<>();
        metaInfo.put("maxOrderCount", maxOrderCount);
        group.setMetaInfo(metaInfo);

        return group;
    }

    @Override
    public void initFromRaw(PerformanceRuleGroup group) {
        setEnabled(group.getEnabled());
        subRules = new ArrayList<>();
        for (PerformanceRule rule : group.getRules()) {
            ValidityBasedRule subRule = new ValidityBasedRule();
            subRule.setPerformance(rule.getPerformance());
            for (PerformanceRuleCondition condition : rule.getConditions()) {
                switch (condition.getKey()) {
                    case "completedTime":
                        if (completedDuration == null) {
                            completedDuration = Range.unCombine(condition, LocalTime::parse);
                        }
                        break;
                    case "orderTimeFromCompleted":
                        subRule.setOrderTimeHour(Range.from(condition, Integer::parseInt));
                        break;
                }
            }
            subRules.add(subRule);
        }

        if (group.getMetaInfo() != null) {
            setMaxOrderCount((Integer) group.getMetaInfo().get("maxOrderCount"));
        }
    }
}
